import React ,{ PropTypes } from 'react'
import { Form, Row, message,  Col, Input, Button, Select, Modal, Message, DatePicker, Icon, Checkbox } from 'antd'
import { fetchPost } from './../request'
import {origin_promotion} from '../../../../utils/config'

const FormItem = Form.Item
const Option = Select.Option
const RangePicker = DatePicker.RangePicker
const CheckboxGroup = Checkbox.Group;

const formItemLayout = {
  labelCol: {span: 6},
  wrapperCol: {span: 18},
};

class Filter extends React.Component {
  constructor (props) {
    super(props)
    this.handleSubmit = this.handleSubmit.bind(this)
    this.handleModelReset = this.handleModelReset.bind(this)
    this.handleSearch = this.handleSearch.bind(this)
    this.resetSearchParmas = this.resetSearchParmas.bind(this)
    this.disabledDate = this.disabledDate.bind(this)
    this.state = {
      data: [],
      batchNO: [],
      startTime: null,
      endTime: null,
      fileList: [],
      params: {
        couponType: '1',
        validtimeType: '1',
        startTime: null,
        endTime: null,
      },
      visible: false,
      batchgrant: false,
    }
  }
  handleSearch() {
    const { validateFields } = this.props.form
    this.props.changeIsLoading()
    validateFields((err, values) => {
      if(!err){
        let aa = {}
        if(values.userName) {
          aa.userName = values.userName;
        }
        if(values.appCode) {
          aa.appCode = values.appCode;
        }
        if(values.status) {
          aa.status = values.status;
        }
        aa.pageSize = 10;
        aa.currentPage = 1;
        this.props.getSearchData(aa)
      }
    })
  }
  handleModelReset() {
    const { resetFields } = this.props.form
    resetFields()
    this.setState({
      visible:false
    })
  }
  handleSubmit () {
    let self = this
    // this.props.changeIsLoading()
    this.props.form.validateFields((err, values) => {
      if(!err) {
        let time = values.time
        let params = {}
        if(values.validtimeType !== '1') {
          values.startTime = time[0].format('YYYY-MM-DD HH:mm:ss'),
            values.endTime = time[1].format('YYYY-MM-DD HH:mm:ss')
          delete values.time
        }
        this.addCoupontemplate(values)
      }
    })
  }

  resetSearchParmas () {
    // this.props.changeIsLoading()
    this.props.form.resetFields()
    this.handleReset()
  }
  handleReset () {
    this.props.form.resetFields()
    this.setState({
      batchgrant: false,
      visible: false,
      fileList: [],
      startTime: null,
      endTime: null,
      params: {
        couponType: '1',
        validtimeType: '1',
        startTime: null,
        endTime: null,
      },
    }, () => {
      this.props.getData()
    })
  }

  disabledDate(current) {
    // Can not select days before today and today
    return current && current.valueOf() < Date.now();
  }


  render() {
    const { getFieldDecorator } = this.props.form
    return (
      <div>
        <Form
          className="ant-advanced-search-form"
          style={{marginBottom: 20}}>
          <Row gutter={40}>
            <Col span={8}>
              <FormItem {...formItemLayout} label={'Name'}>
                {getFieldDecorator('userName')(
                  <Input  placeholder="Please entry name"/>
                )}
              </FormItem>
            </Col>
            <Col span={8}>
              <FormItem {...formItemLayout} label={'Appcode'}>
                {getFieldDecorator('appCode')(
                  <Select
                    placeholder="Please Choose AppCode"
                  >
                    <Option value = {'2'}>Cashmama</Option>
                    <Option value = {'1'}>Other </Option>
                  </Select>
                )}
              </FormItem>
            </Col>
            <Col span={8}>
              <FormItem {...formItemLayout} label={'Status'}>
                {getFieldDecorator('status')(
                  <Select
                    placeholder="Please Choose"
                  >
                    <Option value = {'1'}>Waiting Audit</Option>
                    <Option value = {'3'}>Transfer Success</Option>
                    <Option value = {'5'}>Transfer Failed</Option>
                  </Select>
                )}
              </FormItem>
            </Col>
            <Col span={24} style={{textAlign: 'right'}}>
              <Button type="primary" onClick={this.handleSearch}>Search</Button>
              <Button  style={{marginLeft :6}} onClick={this.resetSearchParmas}>Reset</Button>

            </Col>
          </Row>
        </Form>
      </div>
    )
  }
}

Filter.propTypes = {
  form: PropTypes.object,
  handleSearch: PropTypes.func,
  addList: PropTypes.func,
}

export default Form.create()(Filter)
